package hot100;

public class Solution123 {

    public int maxProfit(int[] prices) {
        int last0 = -prices[0];
        int last1 = 0;
        int last2 = -prices[0];
        int last3 = 0;
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            int current0 = Math.max(last0, -prices[i]);
            int current1 = Math.max(last0 + prices[i], last1);
            int current2 = Math.max(last1 - prices[i], last2);
            int current3 = Math.max(last3, last2 + prices[i]);
            maxProfit = Math.max(Math.max(Math.max(current0, current1), current2), current3);
            last0 = current0;
            last1 = current1;
            last2 = current2;
            last3 = current3;
        }
        return maxProfit;
    }

    public static void main(String[] args) {
        Solution123 solution123 = new Solution123();
        System.out.println(solution123.maxProfit(new int[] {2,1,2,0,1}));
    }
}
